<template>
  <div style="width: 1200px;margin: 0 auto">
    <h1 style="text-align: left;font-size: 40px">领养中心</h1>
    <!--第二行-->
    <el-row gutter="20">
      <el-col span="16">
        <img src="/images/pet_adopt/pet_all.jpg" width="100%">
      </el-col>
      <el-col span="4">
        <!--卡片开始-->
        <el-card class="box-card" style="height: 540px;width: 350px;margin-bottom: 10px">
          <div slot="header" class="clearfix">
            <span style="font-size: 25px; font-style: italic;">作为宠物它有三个原则</span>
          </div>
          <div v-for="a in textArr" class="text item"
               style="font-size: 18px; font-style: italic; margin-bottom: 20px;text-align: left">
            {{ a }}
          </div>
          <img src="/images/pet_adopt/stick_figure.jpg" alt="" width="310px" height="300px">
        </el-card>
        <!--卡片结束-->
      </el-col>
    </el-row>
    <!--动物列表开始-->

    <!--卡片开始-->
    <h1 style="font-size: 30px">待领养宠物在这, 快来领取你的小伙伴吧！</h1>
    <el-row>
      <el-col :span="6" v-for="pet in petArr" :key="pet.id" style="margin: 10px 0">
        <el-card :body-style="{ padding: '0px' }">
          <el-image
              style="width: 200px;"
              :src="'/images/animal/' + pet.pic"
              fit="fit"></el-image>
          <div style="padding: 14px;">
            <span>{{ pet.name }}</span>
            <div class="bottom-clearfix">
              <span>{{ pet.remark }}</span>
            </div>
          </div>
          <el-button @click="loadPetInfo(pet.id)">
            查看详情
          </el-button>
        </el-card>
      </el-col>
    </el-row>
    <!--卡片结束-->
  </div>
</template>

<script>
export default {
  data() {
    return {
      index: "",
      petArr: [],
      petPicArr: [],
      textArr: ["01.按时吃饭", "02.按时吵闹", "03.无论生老病死,对你不离不弃"],
      ads: [],
      a: []
    };
  },
  methods: {
    //跳转到宠物详情
    loadPetInfo(petId) {
      this.$router.push('/sys-user/pet/pet-management/adopt?petId=' + petId);
    },
    //加载宠物列表
    loadPetList() {
      let url = 'http://localhost:13001/pet/list?queryType=all';
      console.log('url = ' + url);
      let localJwt = JSON.parse(localStorage.getItem('localJwt'));
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt.token
            }
          })
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.petArr = jsonResult.data.list;
          if (Array.isArray(this.petPicArr) && Array.isArray(this.petArr)) {
            this.petPicArr.forEach(petPic => {
              let pet = this.petArr.find(pet => pet.id === petPic.petId);
              if (pet) {
                pet.pic = petPic.picUrl;
              }
            });
          }
        }
      });
    },
    //加载宠物图片
    loadPetPiclst() {
      let url = 'http://localhost:13001/picture/listForIsCover';
      console.log('url = ' + url);
      let localJwt = JSON.parse(localStorage.getItem('localJwt'));
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt.token
            }
          })
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.petPicArr = jsonResult.data;
        }
      });
    },
  },
  mounted() {
    this.loadPetPiclst();
    this.loadPetList();

  }
}
</script>

<style scoped>
.bottom-clearfix {
  width: 200px;
  display: inline-block;
  white-space: nowrap; /* 不换行 */
  overflow: hidden; /* 隐藏超出部分 */
  text-overflow: ellipsis;
}
.el-row {
  display: flex;
  flex-wrap: wrap;
}
</style>